<template>
	<div class="system-account-container layout-padding">
		<el-card shadow="hover" :body-style="{ paddingBottom: '0' }">
			<div class="system-account-search mb15">
				<el-input v-model="state.tableData.param.search" size="default" placeholder="角色名称/角色编码" style="max-width: 180px"> </el-input>
				<el-button size="default" type="primary" class="ml10" @click="getTableData()">
					<el-icon>
						<ele-Search />
					</el-icon>
					查询
				</el-button>
				<el-button size="default" type="success" class="ml10" @click="onOpenAddRole('add')">
					<el-icon>
						<ele-FolderAdd />
					</el-icon>
					新增角色
				</el-button>
			</div>
		</el-card>

		<el-card class="full-table" shadow="hover" style="margin-top: 8px">

			<el-table :data="state.tableData.data" v-loading="state.tableData.loading" style="width: 100%" border>
				<el-table-column type="index" label="序号" width="60" />
				<el-table-column prop="name" label="角色名称" show-overflow-tooltip></el-table-column>
				<el-table-column prop="code" label="角色标识" show-overflow-tooltip></el-table-column>
				<el-table-column label="数据范围" align="center" show-overflow-tooltip>
					<template #default="scope">
						<el-tag effect="plain" v-if="scope.row.dataScope === 1">全部数据</el-tag>
						<el-tag effect="plain" v-else-if="scope.row.dataScope === 2">本部门及以下数据</el-tag>
						<el-tag effect="plain" v-else-if="scope.row.dataScope === 3">本部门数据</el-tag>
						<el-tag effect="plain" v-else-if="scope.row.dataScope === 4">仅本人数据</el-tag>
						<el-tag effect="plain" v-else-if="scope.row.dataScope === 5">自定义数据</el-tag>
					</template>
				</el-table-column>
				<el-table-column prop="sortNum" label="排序" show-overflow-tooltip></el-table-column>
				<el-table-column prop="status" label="角色状态" show-overflow-tooltip>
					<template #default="scope">
						<el-tag type="danger" v-if="scope.row.status===1">禁用</el-tag>
						<el-tag type="success" v-else>启用</el-tag>
					</template>
				</el-table-column>
				<el-table-column prop="remarks" label="角色描述" show-overflow-tooltip></el-table-column>
				<el-table-column prop="createTime" label="创建时间" show-overflow-tooltip></el-table-column>
				<el-table-column label="操作" width="150">
					<template #default="scope">
						<el-button :disabled="scope.row.code === 'system'" size="small" text type="primary" @click="onOpenEditRole('edit', scope.row)">修改</el-button>
						<el-button :disabled="scope.row.code === 'system'" size="small" text type="primary" @click="onOpenEditGrant(scope.row)">数据权限</el-button>
						<el-button :disabled="scope.row.code === 'system'" size="small" text type="primary" @click="onRowDel(scope.row)">删除</el-button>
					</template>
				</el-table-column>
			</el-table>
			<el-pagination
				@size-change="onHandleSizeChange"
				@current-change="onHandleCurrentChange"
				class="mt15"
				:pager-count="5"
				:page-sizes="[10, 20, 50, 100]"
				v-model:current-page="state.tableData.param.pageIndex"
				background
				v-model:page-size="state.tableData.param.pageSize"
				layout="total, sizes, prev, pager, next, jumper"
				:total="state.tableData.total">
			</el-pagination>
		</el-card>
		<RoleDialog ref="roleDialogRef" @refresh="getTableData()" />
		<GrantDialog ref="grantDialogRef" @refresh="getTableData()" />
	</div>
</template>

<script setup lang="ts" name="systemAccount">
import { defineAsyncComponent, reactive, onMounted, ref } from 'vue';
import { ElMessageBox, ElMessage } from 'element-plus';
import { useRoleApi } from '/@/api/role';

// 引入组件
const RoleDialog = defineAsyncComponent(() => import('/@/views/system/role/dialog.vue'));
const GrantDialog = defineAsyncComponent(() => import('/@/views/system/role/grant.vue'));

// 定义变量内容
const roleDialogRef = ref();
const grantDialogRef = ref();
const state = reactive({
	tableData: {
		data: [] as Array<RoleForm>,
		total: 0,
		loading: false,
		param: {
			search: '',
			pageIndex: 1,
			pageSize: 10,
		},
	},
});
// 初始化表格数据
const getTableData = () => {
	state.tableData.loading = true;
	//let params = Object.assign(state.tableData.param);
	useRoleApi().getRoleList(state.tableData.param).then(async (res: AjaxResult)=>{
		const _data : Array<RoleForm> = res.data.items;
		//console.log('ok-res:'+JSON.stringify(res.data));
		state.tableData.data = _data; //JSON.parse(JSON.stringify(res.data));
		state.tableData.total = res.data?.totalCount;
		state.tableData.loading = false;
	});
	// const data = [];
	// for (let i = 0; i < 20; i++) {
	// 	data.push({
	// 		roleName: i === 0 ? '超级管理员' : '普通用户',
	// 		roleSign: i === 0 ? 'admin' : 'common',
	// 		describe: `测试角色${i + 1}`,
	// 		sort: i,
	// 		status: true,
	// 		createTime: new Date().toLocaleString(),
	// 	});
	// }
	// state.tableData.data = data;
	// state.tableData.total = state.tableData.data.length;
	setTimeout(() => {
		state.tableData.loading = false;
	}, 500);
};
// 打开新增角色弹窗
const onOpenAddRole = (type: string) => {
	const _row : RoleForm = {status: 0, sortNo: 100 }
	roleDialogRef.value.openDialog(type, _row);
};
// 打开修改角色弹窗
const onOpenEditRole = (type: string, row: RoleForm) => {
	roleDialogRef.value.openDialog(type, row);
};
// 删除角色
const onRowDel = (row: RowRoleType) => {
	ElMessageBox.confirm(`此操作将永久删除角色名称：“${row.roleName}”，是否继续?`, '提示', {
		confirmButtonText: '确认',
		cancelButtonText: '取消',
		type: 'warning',
	})
	.then(() => {
		getTableData();
		ElMessage.success('删除成功');
	})
	.catch(() => {});
};
// 打开修改角色弹窗
const onOpenEditGrant = (row: RoleForm) => {
	grantDialogRef.value.openDialog(row);
};
// 分页改变
const onHandleSizeChange = (val: number) => {
	state.tableData.param.pageSize = val;
	getTableData();
};
// 分页改变
const onHandleCurrentChange = (val: number) => {
	state.tableData.param.pageIndex = val;
	getTableData();
};
// 页面加载时
onMounted(() => {
	getTableData();
});
</script>

<style scoped lang="scss">
.system-account-container {
	.system-account-padding {
		padding: 15px;
		.el-table {
			flex: 1;
		}
	}
}
</style>
